package fr.upmc.i3s.upmcontact.views.fragments;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import fr.upmc.i3s.upmcontact.R;
import fr.upmc.i3s.upmcontact.backend.BackEndBridgeImpl;
import fr.upmc.i3s.upmcontact.tools.StringUtils;
import fr.upmc.i3s.upmcontact.views.FragmentController;
import fr.upmc.i3s.upmcontact.views.fragments.models.IFragmentModel;
import fr.upmc.i3s.upmcontact.views.fragments.models.PageFragmentModel;

public class ThanksFragment extends PageFragmentModel implements IFragmentModel {
	
	private static final String EXTRA_MSG = "extra.param.msg";
	
	private FragmentController fragmentController;
	private static volatile ThanksFragment INSTANCE;
	
	private TextView tvMessage;
	private TextView tvFeetLeft;
	private TextView tvFeetRight;
	private ImageView ivArrowLeft;
	private ImageView ivArrowRight;
	
	private int mMsgId;
	
	public static ThanksFragment newInstance(int headIconRes, int msgId) {
		if(INSTANCE == null) {
			ThanksFragment fragment = new ThanksFragment();
		    Bundle bundle = new Bundle(4);
		    bundle.putInt(EXTRA_LAYOUT_FRAGMENT, R.layout.fragment_thanks);
		    bundle.putInt(EXTRA_HEAD_ICON, headIconRes);
		    bundle.putInt(EXTRA_HEAD_TEXT, R.string.title_fragment_thanks);
		    bundle.putInt(EXTRA_MSG, msgId);
		    fragment.setArguments(bundle);
		    INSTANCE = fragment;
		}
	    return INSTANCE;
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (savedInstanceState != null) {
        	mMsgId = savedInstanceState.getInt(EXTRA_MSG);
        } else {
            if(getArguments() != null) {
            	mMsgId = getArguments().getInt(EXTRA_MSG);
            }
        }
		return super.onCreateView(inflater, container, savedInstanceState);
	}
	
 	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		super.initFragmentViews();
		this.fragmentController = (FragmentController) getActivity();
		fetchViews();
		customizeViews();
		setListeners();
	}
 	
 	@Override
 	public void fetchViews() {
 		this.tvMessage = (TextView) this.getView().findViewById(R.id.textView_thanks_msg);
 		this.tvFeetLeft = (TextView) this.getView().findViewById(R.id.textView_feet_text_left);
 		this.tvFeetRight = (TextView) this.getView().findViewById(R.id.textView_feet_text_right);
 		this.ivArrowLeft = (ImageView) this.getView().findViewById(R.id.imageView_feet_arrow_left);
 		this.ivArrowRight = (ImageView) this.getView().findViewById(R.id.imageView_feet_arrow_right);
 	}
 	
 	@Override
 	public void customizeViews() {
 		if(BackEndBridgeImpl.getInstance(fragmentController).getCurrentUser() != null) {
 			this.tvMessage.setText(fragmentController.getString(mMsgId)+", "
 						+StringUtils.toProperNoun(BackEndBridgeImpl.getInstance(fragmentController).getCurrentUser().getUsername())+" !");
 		} else {
 			this.tvMessage.setText(fragmentController.getString(mMsgId)+" !");
 		}
 		this.tvFeetLeft.setText("");
 		this.tvFeetRight.setText("");
 		this.ivArrowLeft.setVisibility(View.GONE);
 		this.ivArrowRight.setVisibility(View.GONE);
 	}
 	
 	@Override
 	public void setListeners() {
 	}
 	
}
